package com.ggs.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class JWTUtil {
    private static final String KEY = "ggs";
    private static final Algorithm  ALGORITHM = Algorithm.HMAC256(KEY);
    private static final Long EXPIRATION_TIME = 1000L * 60 * 60;

    // 生成Token并返回
    public static String genToken(HashMap<String, Object> claims){
        String token = JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .sign(ALGORITHM);
        return token;
    }

    // 解析Token
    public static Map<String, Object> parseToken(String token){
        Map<String, Object> claims = JWT.require(ALGORITHM)
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
        return claims;
    }
}
